﻿Imports System.IO

Public Class CApplicationManager
    Public Declare Function GetExitCodeProcess Lib "kernel32.dll" (ByVal hProcess As Integer, ByVal lpExitCode As Integer) As Integer
    Private Shared m_instance As CApplicationManager = New CApplicationManager()

    Private Const SETTING_PATH = "master\"
    Private Const SETTING_FILE = SETTING_PATH & "setting.config"

    Public m_AppSetting As CApplicationSettings
    Public m_PID As Integer

    Public Enum EnumSpecialFolder
        PATH_APPLICATION
        PATH_MYDOCUMENT
        PATH_TEMP
        PATH_WINDOWS
    End Enum

    Public Sub New()
        Dim filename As String = GetRealPath("master\", EnumSpecialFolder.PATH_MYDOCUMENT)
        Dim curProcess As Process = Process.GetCurrentProcess()
        m_PID = curProcess.Id
        Debug.WriteLine("m_PID:[" & m_PID & "]")

        m_AppSetting = LoadFromXmlFile()
        If m_AppSetting Is Nothing Then
            m_AppSetting = New CApplicationSettings()
        End If
    End Sub

    Public Shared Function GetInstance() As CApplicationManager
        Return m_instance
    End Function

    Public Shared Function GetRealPath(ByVal FileName As String, ByVal type As EnumSpecialFolder) As String
        Dim pathFile As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
        Select Case type
            Case EnumSpecialFolder.PATH_MYDOCUMENT
                Return Path.Combine(pathFile, FileName).Replace("file:\", "")
            Case EnumSpecialFolder.PATH_TEMP
                Return Path.GetTempFileName.Replace("file:\", "")
            Case EnumSpecialFolder.PATH_APPLICATION
                Dim strAppDir As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
                Return System.IO.Path.Combine(strAppDir, FileName).Replace("file:\", "")
        End Select
        Return Nothing
    End Function


    Public Function GetFileMD5(ByVal FileName As String)
        Dim Reader As New System.IO.StreamReader(FileName, System.Text.Encoding.GetEncoding("UTF-8"))
        Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider()
        Dim bs As Byte() = md5.ComputeHash(Reader.BaseStream)
        Dim result As String = BitConverter.ToString(bs).ToLower().Replace("-", "")
        Debug.WriteLine("FileName[" & FileName & "]:MD5:[" & result & "]")
        Reader.Close()
        Return result
    End Function



    ''' <summary>
    ''' 設定をXMLファイルから保存する
    ''' </summary>
    Public Shared Sub SaveToXmlFile(ByVal settings As CApplicationSettings)
        Dim path As String = GetRealPath(SETTING_PATH, EnumSpecialFolder.PATH_MYDOCUMENT)
        Dim filename As String = GetRealPath(SETTING_FILE, EnumSpecialFolder.PATH_MYDOCUMENT)
        Dim fs As FileStream = Nothing
        Dim xs As System.Xml.Serialization.XmlSerializer = Nothing

        If File.Exists(filename) = False Then
            Directory.CreateDirectory(path)
        End If

        Try
            fs = New FileStream(filename, FileMode.Create, FileAccess.Write)
            xs = New System.Xml.Serialization.XmlSerializer(GetType(CApplicationSettings))
            Debug.WriteLine("SaveFile:" & filename)
            xs.Serialize(fs, settings)
        Catch ex As Exception
            Debug.WriteLine("Exception:" & ex.ToString)
        Finally
            fs.Close()
        End Try


    End Sub


    ''' <summary>
    ''' 設定をXMLファイルから読み込み復元する
    ''' </summary>
    Public Shared Function LoadFromXmlFile() As CApplicationSettings
        Dim path As String = GetRealPath(SETTING_PATH, EnumSpecialFolder.PATH_MYDOCUMENT)
        Dim filename As String = GetRealPath(SETTING_FILE, EnumSpecialFolder.PATH_MYDOCUMENT)
        Dim fs As FileStream = Nothing
        Dim xs As System.Xml.Serialization.XmlSerializer = Nothing
        Dim obj As Object = Nothing

        If File.Exists(filename) = False Then
            Debug.WriteLine("ファイルが存在しません。:" & filename)
            Return Nothing
        End If

        Try
            fs = New FileStream(filename, FileMode.Open, FileAccess.Read)
            xs = New System.Xml.Serialization.XmlSerializer(GetType(CApplicationSettings))
            Debug.WriteLine("LoadFile:" & filename)
            obj = xs.Deserialize(fs)
            fs.Close()
        Catch ex As Exception
            Debug.WriteLine("Exception:" & ex.ToString)
        End Try

        Return CType(obj, CApplicationSettings)
    End Function

    ''' <summary>
    ''' アプリケーション情報
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetAppSettings() As CApplicationSettings
        Return m_AppSetting
    End Function


    ''' <summary>
    ''' 更新プログラムの実行
    ''' </summary>
    ''' <param name="fileName"></param>
    ''' <remarks></remarks>
    Public Sub ExecuteUpdateProcess(ByVal fileName As String)
        Dim info As New ProcessStartInfo
        'info.FileName = GetRealPath(fileName, EnumSpecialFolder.PATH_APPLICATION)
        Dim StrFileName As String = GetRealPath(fileName, EnumSpecialFolder.PATH_APPLICATION).Replace("\update", "")
        info.FileName = StrFileName
        info.Arguments = CStr(m_PID)
        Dim p As System.Diagnostics.Process = System.Diagnostics.Process.Start(info)
    End Sub

    ''' <summary>
    ''' 実行中の同じアプリケーションのプロセスを取得する
    ''' </summary>
    ''' <param name="id"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetProcess(ByVal id As Integer) As Process
        Dim p As Process = Process.GetProcessById(id)
        Return p
    End Function


End Class

''' <summary>
''' アプリケーション設定情報
''' </summary>
''' <remarks></remarks>
Public Class CApplicationSettings
    Private m_ServerAddress As String = ""
    Private m_UserId As String = ""
    Private m_UserPassword As String = ""
    Private m_IsInnerLabelCheck As Boolean = False
    Private m_IsMazdaAlertCheck As Boolean = True
    Private m_IsStaticReadEmpCd As Boolean = True
    Private m_IsBoxEndChk As Boolean = True
    Private m_IsMazdaAlarmChk As Boolean = True
    Private m_IsListDeleteChk As Boolean = True
    Private m_UniqueMonth As Integer = 0

    Public Property ServerAddress() As String
        Get
            Return m_ServerAddress
        End Get
        Set(ByVal value As String)
            m_ServerAddress = value
        End Set
    End Property

    Public Property UserId() As String
        Get
            Return m_UserId
        End Get
        Set(ByVal value As String)
            m_UserId = value
        End Set
    End Property

    Public Property UserPassword() As String
        Get
            Return m_UserPassword
        End Get
        Set(ByVal value As String)
            m_UserPassword = value
        End Set
    End Property

    Public Property IsInnerLabelCheck() As Boolean
        Get
            Return m_IsInnerLabelCheck
        End Get
        Set(ByVal value As Boolean)
            m_IsInnerLabelCheck = value
        End Set
    End Property

    Public Property IsMazdaAlertCheck() As Boolean
        Get
            Return m_IsMazdaAlertCheck
        End Get
        Set(ByVal value As Boolean)
            m_IsMazdaAlertCheck = value
        End Set
    End Property
    Public Property IsListDeleteChk() As Boolean
        Get
            Return m_IsListDeleteChk
        End Get
        Set(ByVal value As Boolean)
            m_IsListDeleteChk = value
        End Set
    End Property
    Public Property IsStaticReadEmpCd() As Boolean
        Get
            Return m_IsStaticReadEmpCd
        End Get
        Set(ByVal value As Boolean)
            m_IsStaticReadEmpCd = value
        End Set
    End Property
    Public Property IsBoxEndChk() As Boolean
        Get
            Return m_IsBoxEndChk
        End Get
        Set(ByVal value As Boolean)
            m_IsBoxEndChk = value
        End Set
    End Property
    Public Property IsMazdaAlarmChk() As Boolean
        Get
            Return m_IsMazdaAlarmChk
        End Get
        Set(ByVal value As Boolean)
            m_IsMazdaAlarmChk = value
        End Set
    End Property

    Public Property UniqueMonth() As Integer
        Get
            Return m_UniqueMonth
        End Get
        Set(ByVal value As Integer)
            m_UniqueMonth = value
        End Set
    End Property

End Class



Public Class BorderLabel : Inherits System.Windows.Forms.Label

End Class
